/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainWindows.java
 *
 * Created on Sep 9, 2010, 4:21:48 PM
 */
package eproject2;

import java.awt.event.KeyEvent;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Box;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Hoang
 */
//--------------------Thread 1 -----------------------------//
class Filter implements Runnable{
    ResultSet rs;
    DefaultTableModel threadtablemode;
    String strfilter;

    public Filter(ResultSet rs, DefaultTableModel threadtablemode, String strfilter) {
        this.rs = rs;
        this.threadtablemode = threadtablemode;
        this.strfilter = strfilter.toLowerCase();
    }

    public void setThreadtablemode(DefaultTableModel threadtablemode) {
        this.threadtablemode = threadtablemode;
    }
    
    public void run() {
        try {
            rs.beforeFirst();
            do {
                rs.next();
                if (rs.getString("Name").toLowerCase().contains(this.strfilter)) {
                    Vector vt = new Vector();
                    vt.add(rs.getString("Name"));
                    vt.add(rs.getString("Address"));
                    vt.add(rs.getString("homephone"));
                    vt.add(rs.getString("email"));
                    vt.add(rs.getString("workphone"));
                    vt.add(rs.getString("cellno"));
                    vt.add(rs.getString("homepage"));
                    vt.add(rs.getDate("birthday"));
                    vt.add(rs.getInt("CatID"));
                    vt.add(rs.getInt("ContactID"));
                    threadtablemode.addRow(vt);
                }
            } while (!rs.isAfterLast());
        } catch (SQLException ex) {
            Logger.getLogger(Filter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
public class MainWindows extends javax.swing.JFrame {

    /** Creates new form MainWindows */
    static SQLConnects sqlconnection;
    DefaultTableModel tablemodel;
    Connection conn;
    Statement stmt;
    String urlString, driverString;
    ResultSet rs;
    PreparedStatement pst;

    public MainWindows() {
        tablemodel = new DefaultTableModel();
        String[] rowData = new String[]{"Full Name", "Address", "Home Phone", "Email", "Birthdate", "Work Phone", "Cell number", "Home page", "Category"};
        tablemodel.addColumn("Full Name");
        tablemodel.addColumn("Address");
        tablemodel.addColumn("Home Phone");
        tablemodel.addColumn("Email");
        tablemodel.addColumn("Work Phone");
        tablemodel.addColumn("Cell number");
        tablemodel.addColumn("Home page");
        tablemodel.addColumn("Birthdate");
        tablemodel.addColumn("Category");
        tablemodel.addColumn("ID");
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        tbaddcontact = new javax.swing.JButton();
        tbeditcontact = new javax.swing.JButton();
        tbdelcontact = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtfilter = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jToggleButton1 = new javax.swing.JToggleButton();
        btAdd = new javax.swing.JButton();
        btEdit = new javax.swing.JButton();
        btDelete = new javax.swing.JButton();
        btView = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane(jTBContact);
        jTBContact = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButton12 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextField8 = new javax.swing.JTextField();
        jButton19 = new javax.swing.JButton();
        jButton21 = new javax.swing.JButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jToggleButton3 = new javax.swing.JToggleButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jTextField4 = new javax.swing.JTextField();
        jCheckBox3 = new javax.swing.JCheckBox();
        jTextField5 = new javax.swing.JTextField();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jCheckBox4 = new javax.swing.JCheckBox();
        jTextField6 = new javax.swing.JTextField();
        jButton16 = new javax.swing.JButton();
        jButton17 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable3 = new javax.swing.JTable();
        jToolBar2 = new javax.swing.JToolBar();
        tbaddcategory = new javax.swing.JButton();
        tbeditcategory = new javax.swing.JButton();
        tbdelcategory = new javax.swing.JButton();
        jToolBar3 = new javax.swing.JToolBar();
        tbsavetohtml = new javax.swing.JButton();
        tbPrinter = new javax.swing.JButton();
        txttbSearch = new javax.swing.JTextField();
        bttbSearch = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnunewcontact = new javax.swing.JMenuItem();
        mnunewCategory = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnusavetoHTML = new javax.swing.JMenuItem();
        mnuprinter = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        mnuexit = new javax.swing.JMenuItem();
        mnucopy = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        mnuPaste = new javax.swing.JMenuItem();
        mnuDelete = new javax.swing.JMenuItem();
        mnuDeleteAll = new javax.swing.JMenuItem();
        mnuSelectall = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        mnuSearch = new javax.swing.JMenuItem();
        mnuSQLSetting = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setRollover(true);

        tbaddcontact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Addcontact_32.png"))); // NOI18N
        tbaddcontact.setToolTipText("Add new contact");
        tbaddcontact.setFocusable(false);
        tbaddcontact.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbaddcontact.setMaximumSize(new java.awt.Dimension(39, 39));
        tbaddcontact.setMinimumSize(new java.awt.Dimension(39, 39));
        tbaddcontact.setPreferredSize(new java.awt.Dimension(39, 39));
        tbaddcontact.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbaddcontact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbaddcontactActionPerformed(evt);
            }
        });
        jToolBar1.add(tbaddcontact);

        tbeditcontact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Editcontact_32.png"))); // NOI18N
        tbeditcontact.setToolTipText("Edit Contact Info");
        tbeditcontact.setEnabled(false);
        tbeditcontact.setFocusable(false);
        tbeditcontact.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbeditcontact.setMaximumSize(new java.awt.Dimension(39, 39));
        tbeditcontact.setMinimumSize(new java.awt.Dimension(39, 39));
        tbeditcontact.setPreferredSize(new java.awt.Dimension(39, 39));
        tbeditcontact.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(tbeditcontact);

        tbdelcontact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/DeleteContact_32.png"))); // NOI18N
        tbdelcontact.setToolTipText("Delete Contact");
        tbdelcontact.setEnabled(false);
        tbdelcontact.setFocusable(false);
        tbdelcontact.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbdelcontact.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tbdelcontact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tbdelcontactActionPerformed(evt);
            }
        });
        jToolBar1.add(tbdelcontact);

        jTabbedPane1.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);
        jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        jLabel1.setText("Filter");

        txtfilter.setText("Filter Word");
        txtfilter.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtfilterKeyPressed(evt);
            }
        });

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Full Name", "Birthdate", "Address", "Email", "Home Phone" }));

        jToggleButton1.setText("Refresh");

        btAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Addcontact_32.png"))); // NOI18N
        btAdd.setText("Add");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });

        btEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Editcontact_32.png"))); // NOI18N
        btEdit.setText("Edit");
        btEdit.setEnabled(false);
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });

        btDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/DeleteContact_32.png"))); // NOI18N
        btDelete.setText("Delete");
        btDelete.setEnabled(false);
        btDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btDeleteActionPerformed(evt);
            }
        });

        btView.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/View_32.png"))); // NOI18N
        btView.setText("View Details");
        btView.setEnabled(false);
        btView.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btViewActionPerformed(evt);
            }
        });

        jScrollPane1.setSize(500, 500);

        jTBContact.setModel(tablemodel);
        jTBContact.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        jTBContact.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTBContactMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTBContact);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(txtfilter, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jToggleButton1))
                        .addGroup(jPanel2Layout.createSequentialGroup()
                            .addComponent(btAdd)
                            .addGap(30, 30, 30)
                            .addComponent(btEdit)
                            .addGap(39, 39, 39)
                            .addComponent(btDelete)
                            .addGap(44, 44, 44)
                            .addComponent(btView))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtfilter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToggleButton1))
                .addGap(11, 11, 11)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btAdd)
                    .addComponent(btEdit)
                    .addComponent(btDelete)
                    .addComponent(btView))
                .addContainerGap(46, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("<html><img alt=\"test\"  src=\"file:/C:/Documents%20and%20Settings/Hoang/My%20Documents/NetBeansProjects/EProject2/src/images/Contact.png\"/>Contact</html>", jPanel2);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "ID", "Category Name", "Number of Contacts", "Notes"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jButton12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CategoryDelete.png"))); // NOI18N
        jButton12.setText("Delete Category");

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Edit New Category"));

        jLabel2.setText("Category Name");

        jTextField7.setText("Family");

        jLabel4.setText("Notes");

        jTextField8.setText("Family 's contact");

        jButton19.setText("Save");

        jButton21.setText("Cancel");

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField8)
                            .addComponent(jTextField7, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)))
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addGap(64, 64, 64)
                        .addComponent(jButton19)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 139, Short.MAX_VALUE)
                        .addComponent(jButton21)))
                .addContainerGap(152, Short.MAX_VALUE))
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton19)
                    .addComponent(jButton21))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        jToggleButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CategoryAdd_32.png"))); // NOI18N
        jToggleButton2.setText("Add new Category");

        jToggleButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CategoryEdit_32.png"))); // NOI18N
        jToggleButton3.setText("Edit Category");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(jToggleButton2)
                        .addGap(31, 31, 31)
                        .addComponent(jToggleButton3)
                        .addGap(50, 50, 50)
                        .addComponent(jButton12))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jPanel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(63, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jToggleButton2)
                    .addComponent(jButton12)
                    .addComponent(jToggleButton3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(124, 124, 124))
        );

        jTabbedPane1.addTab("<html><img src=\"file:/C:/Documents%20and%20Settings/Hoang/My%20Documents/NetBeansProjects/EProject2/src/images/Category.png\"/>Category</html>", jPanel3);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Search Advance"));

        jLabel3.setText("Category");

        jTextField3.setText("Put Name Here");

        jCheckBox1.setText("Name");

        jCheckBox2.setText("Phone Number");

        jTextField4.setText("Put Phone Here");

        jCheckBox3.setText("Address");

        jTextField5.setText("Put Address Here");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Uncategory", "Friends", "Family", "New Customers", "Group 2" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane3.setViewportView(jList1);

        jCheckBox4.setText("Email");

        jTextField6.setText("Put Email Here");

        jButton16.setText("Search");

        jButton17.setText("Clear");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox2)
                            .addComponent(jCheckBox3)
                            .addComponent(jCheckBox1)
                            .addComponent(jCheckBox4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField6, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                            .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE)
                            .addComponent(jTextField5, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jButton16)
                        .addGap(59, 59, 59)
                        .addComponent(jButton17)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBox1)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBox2)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jCheckBox3)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox4)
                    .addComponent(jTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton16)
                    .addComponent(jButton17)))
        );

        jTable3.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Full Name", "Birth Date", "Adress", "Email", "Home Page", "Home Phone", "Work Phone", "Cell Number", "Remarks            ", "Category"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane4.setViewportView(jTable3);
        jTable3.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("<html><br><br><img src=\"file:/C:/Documents%20and%20Settings/Hoang/My%20Documents/NetBeansProjects/EProject2/src/images/SearchAdvance.png\"/>Search Advance</html>", jPanel4);

        jToolBar2.setFloatable(false);
        jToolBar2.setRollover(true);

        tbaddcategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CategoryAdd_32.png"))); // NOI18N
        tbaddcategory.setToolTipText("Add Category");
        tbaddcategory.setFocusable(false);
        tbaddcategory.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbaddcategory.setMaximumSize(new java.awt.Dimension(39, 39));
        tbaddcategory.setMinimumSize(new java.awt.Dimension(39, 39));
        tbaddcategory.setPreferredSize(new java.awt.Dimension(39, 39));
        tbaddcategory.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(tbaddcategory);

        tbeditcategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CategoryEdit_32.png"))); // NOI18N
        tbeditcategory.setToolTipText("Rename Category");
        tbeditcategory.setEnabled(false);
        tbeditcategory.setFocusable(false);
        tbeditcategory.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbeditcategory.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(tbeditcategory);

        tbdelcategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CategoryDelete.png"))); // NOI18N
        tbdelcategory.setToolTipText("Delete Category");
        tbdelcategory.setEnabled(false);
        tbdelcategory.setFocusable(false);
        tbdelcategory.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbdelcategory.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(tbdelcategory);

        jToolBar3.setFloatable(false);
        jToolBar3.setRollover(true);

        tbsavetohtml.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/SaveToHTML_32.png"))); // NOI18N
        tbsavetohtml.setEnabled(false);
        tbsavetohtml.setFocusable(false);
        tbsavetohtml.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbsavetohtml.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar3.add(tbsavetohtml);

        tbPrinter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Printer_32.png"))); // NOI18N
        tbPrinter.setEnabled(false);
        tbPrinter.setFocusable(false);
        tbPrinter.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tbPrinter.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar3.add(tbPrinter);

        txttbSearch.setText("Search Contact");

        bttbSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Search_16.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jToolBar3, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(125, 125, 125)
                .addComponent(txttbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bttbSearch)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 726, Short.MAX_VALUE)
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jToolBar1, jToolBar2, jToolBar3});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(bttbSearch)
                            .addComponent(txttbSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jToolBar1, jToolBar2, jToolBar3});

        jMenu1.setMnemonic(KeyEvent.VK_F);
        jMenu1.setText("File");

        mnunewcontact.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnunewcontact.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Addcontact_16.png"))); // NOI18N
        mnunewcontact.setMnemonic(KeyEvent.VK_N);
        mnunewcontact.setText("Create New Contact");
        mnunewcontact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnunewcontactActionPerformed(evt);
            }
        });
        jMenu1.add(mnunewcontact);

        mnunewCategory.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        mnunewCategory.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/CategoryAdd_16.png"))); // NOI18N
        mnunewCategory.setMnemonic(KeyEvent.VK_G);
        mnunewCategory.setText("Create New Category");
        jMenu1.add(mnunewCategory);
        jMenu1.add(jSeparator1);

        mnusavetoHTML.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        mnusavetoHTML.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/SaveToHTML_16.png"))); // NOI18N
        mnusavetoHTML.setText("Save to HTML");
        jMenu1.add(mnusavetoHTML);

        mnuprinter.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        mnuprinter.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Printer_16.png"))); // NOI18N
        mnuprinter.setText("Print A Contact Info");
        jMenu1.add(mnuprinter);
        jMenu1.add(jSeparator2);

        mnuexit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        mnuexit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Exit_16.png"))); // NOI18N
        mnuexit.setText("Exit");
        mnuexit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuexitActionPerformed(evt);
            }
        });
        jMenu1.add(mnuexit);

        jMenuBar1.add(jMenu1);

        mnucopy.setText("Edit");
        mnucopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnucopyActionPerformed(evt);
            }
        });

        jMenuItem6.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem6.setText("Copy");
        mnucopy.add(jMenuItem6);

        mnuPaste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        mnuPaste.setText("Paste");
        mnucopy.add(mnuPaste);

        mnuDelete.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        mnuDelete.setText("Delete");
        mnuDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuDeleteActionPerformed(evt);
            }
        });
        mnucopy.add(mnuDelete);

        mnuDeleteAll.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        mnuDeleteAll.setText("Delete All");
        mnucopy.add(mnuDeleteAll);

        mnuSelectall.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        mnuSelectall.setText("Select All");
        mnucopy.add(mnuSelectall);
        mnucopy.add(jSeparator3);

        mnuSearch.setText("Search");
        mnucopy.add(mnuSearch);

        mnuSQLSetting.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSQLSettingActionPerformed(evt);
            }
        });
        mnucopy.add(mnuSQLSetting);

        jMenuBar1.add(mnucopy);
        jMenuBar1.add(Box.createHorizontalGlue());
        jMenuBar1.add(Box.createHorizontalGlue());
        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuexitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuexitActionPerformed
        // TODO add your handling code here:
        System.exit(-1);
    }//GEN-LAST:event_mnuexitActionPerformed

    private void mnucopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnucopyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mnucopyActionPerformed

    private void mnuSQLSettingActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSQLSettingActionPerformed
        // TODO add your handling code here:
        dlgDatabaseConfig dlgconfig = new dlgDatabaseConfig(this, true);
        dlgconfig.setVisible(true);
    }//GEN-LAST:event_mnuSQLSettingActionPerformed

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        // TODO add your handling code here:
        dlgAddContact addnew = new dlgAddContact(this, true);
        addnew.setVisible(true);
    }//GEN-LAST:event_btAddActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed

        if (jTBContact.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "Please Choose one row data to edit");
            return;
        }
        int rowselected = jTBContact.getSelectedRow();
        dlgEditContact editdialog = new dlgEditContact(this, true, getContact(rowselected));
        ;
        editdialog.setContactupdate(getContact(rowselected));
        editdialog.setVisible(true);
    }//GEN-LAST:event_btEditActionPerformed

    private void tbaddcontactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbaddcontactActionPerformed
        // TODO add your handling code here:
        dlgAddContact addnew = new dlgAddContact(this, true);
        addnew.setVisible(true);
    }//GEN-LAST:event_tbaddcontactActionPerformed

    private void jTBContactMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTBContactMouseClicked
        // TODO add your handling code here:
        btEdit.setEnabled(true);
        btDelete.setEnabled(true);
        tbeditcontact.setEnabled(true);
        tbdelcontact.setEnabled(true);
        btView.setEnabled(true);
    }//GEN-LAST:event_jTBContactMouseClicked

    private void btDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDeleteActionPerformed
        deletecontact();
    }//GEN-LAST:event_btDeleteActionPerformed

    private void tbdelcontactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbdelcontactActionPerformed
        deletecontact();
    }//GEN-LAST:event_tbdelcontactActionPerformed

    private void mnuDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuDeleteActionPerformed
        deletecontact();
    }//GEN-LAST:event_mnuDeleteActionPerformed

    private void mnunewcontactActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnunewcontactActionPerformed
        // TODO add your handling code here:
        dlgAddContact addnew = new dlgAddContact(this, true);
        addnew.setVisible(true);
    }//GEN-LAST:event_mnunewcontactActionPerformed

    private void btViewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btViewActionPerformed
        // TODO add your handling code here:
        if (jTBContact.getSelectedRowCount()>1){
            JOptionPane.showMessageDialog(this, "Choose one row only");
            return;
        }
        Contact contactview=getContact(jTBContact.getSelectedRow());
        dlgViewDetails viewdlg = new dlgViewDetails(this, true, contactview);
        viewdlg.setVisible(true);
    }//GEN-LAST:event_btViewActionPerformed

    private void txtfilterKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtfilterKeyPressed
            // TODO add your handling code here:
                rs=SQLexcute("Select * from Contact");
                Filter filter = new Filter(rs, tablemodel, txtfilter.getText().trim());
                filter.setThreadtablemode(tablemodel);
                Thread tfilter = new Thread(filter);
                tfilter.run();
    }//GEN-LAST:event_txtfilterKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                MainWindows mainframe = new MainWindows();
                sqlconnection = new SQLConnects();
                if (!sqlconnection.checksetting()) {
                    dlgDatabaseConfig dlgconfig = new dlgDatabaseConfig(mainframe, true);
                    dlgconfig.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(-1);
                        }
                    });
                    dlgconfig.setVisible(true);
                } else {
                    sqlconnection = sqlconnection.getinfo();
                    mainframe.initconnect(sqlconnection);
                    mainframe.refreshtable();
                    mainframe.setVisible(true);
                }
                mainframe.setLocationRelativeTo(null);
                mainframe.pack();
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btDelete;
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btView;
    private javax.swing.JButton bttbSearch;
    private javax.swing.JButton jButton12;
    private javax.swing.JButton jButton16;
    private javax.swing.JButton jButton17;
    private javax.swing.JButton jButton19;
    private javax.swing.JButton jButton21;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JTable jTBContact;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTable3;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField6;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JToolBar jToolBar3;
    private javax.swing.JMenuItem mnuDelete;
    private javax.swing.JMenuItem mnuDeleteAll;
    private javax.swing.JMenuItem mnuPaste;
    private javax.swing.JMenuItem mnuSQLSetting;
    private javax.swing.JMenuItem mnuSearch;
    private javax.swing.JMenuItem mnuSelectall;
    private javax.swing.JMenu mnucopy;
    private javax.swing.JMenuItem mnuexit;
    private javax.swing.JMenuItem mnunewCategory;
    private javax.swing.JMenuItem mnunewcontact;
    private javax.swing.JMenuItem mnuprinter;
    private javax.swing.JMenuItem mnusavetoHTML;
    private javax.swing.JButton tbPrinter;
    private javax.swing.JButton tbaddcategory;
    private javax.swing.JButton tbaddcontact;
    private javax.swing.JButton tbdelcategory;
    private javax.swing.JButton tbdelcontact;
    private javax.swing.JButton tbeditcategory;
    private javax.swing.JButton tbeditcontact;
    private javax.swing.JButton tbsavetohtml;
    private javax.swing.JTextField txtfilter;
    private javax.swing.JTextField txttbSearch;
    // End of variables declaration//GEN-END:variables

    public void initconnect(SQLConnects connect) {
        try {
            System.out.println("Connecting ....");
            urlString = "jdbc:sqlserver://" + connect.server + ":" + connect.port + ";DatabaseName=" + connect.database + ";User=" + connect.username + ";Password=" + connect.password + ";";
            driverString = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            Class.forName(driverString);
            conn = DriverManager.getConnection(urlString);
            System.out.println("Connected to Database ....");
        } catch (SQLException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

//    public Vector getresultsetCat(){
//        try {
//            Vector tempv=new Vector();
//            urlString = "Select * from Categories";
//            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//            ResultSet temprs=stmt.executeQuery(urlString);
//            while (temprs.next()){
//                tempv.add((String) temprs.getString(2));
//            }
//            return tempv;
//        } catch (SQLException ex) {
//            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
//            return null;
//        }
//    }
    public void refreshtable() {
        try {
            urlString = "Select * from Contact";
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = stmt.executeQuery(urlString);
            while (rs.next()) {
                Vector v = new Vector();
                v.add(rs.getString("Name"));
                v.add(rs.getString("Address"));
                v.add(rs.getString("homephone"));
                v.add(rs.getString("email"));
                v.add(rs.getString("workphone"));
                v.add(rs.getString("cellno"));
                v.add(rs.getString("homepage"));
                v.add(rs.getDate("birthday"));
                v.add(rs.getInt("CatID"));
                v.add(rs.getInt("ContactID"));
                tablemodel.addRow(v);
                jTBContact.setModel(tablemodel);
            }
        } catch (SQLException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addnewcontact(Vector contactvector) {
        tablemodel.addRow(contactvector);
    }

    private int seek(int ContactID) {
        try {
            rs.beforeFirst();
            int i = 0;
            do {
                rs.next();
                if (rs.getInt("ContactID") == ContactID) {
                    break;
                }
                i++;
            } while (!rs.isLast());
            return i;
        } catch (SQLException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }
    }

    public Contact getContact(int row) {
        try {
            Contact tmp = new Contact();
            rs.absolute(seek((Integer) jTBContact.getValueAt(row, 9)));
            rs.next();
            tmp.setFullname(rs.getString("Name"));
            tmp.setAddress(rs.getString("Address"));
            tmp.setHomephone(rs.getString("homephone"));
            tmp.setEmail(rs.getString("email"));
            tmp.setWorkphone(rs.getString("workphone"));
            tmp.setCellphone(rs.getString("cellno"));
            tmp.setHomepage(rs.getString("homepage"));
            tmp.setBirthday(rs.getDate("birthday"));
            tmp.setCatID(rs.getInt("CatID"));
            tmp.ID = rs.getInt("ContactID");
            return tmp;
        } catch (SQLException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void updaterow(Contact contactupdate) {
        int row = jTBContact.getSelectedRow();
        tablemodel.setValueAt(contactupdate.getFullname(), row, 0);
        tablemodel.setValueAt(contactupdate.getAddress(), row, 1);
        tablemodel.setValueAt(contactupdate.getHomephone(), row, 2);
        tablemodel.setValueAt(contactupdate.getEmail(), row, 3);
        tablemodel.setValueAt(contactupdate.getWorkphone(), row, 4);
        tablemodel.setValueAt(contactupdate.getCellphone(), row, 5);
        tablemodel.setValueAt(contactupdate.getHomepage(), row, 6);
        tablemodel.setValueAt(contactupdate.getBirthday(), row, 7);
        tablemodel.setValueAt(contactupdate.getCatID(), row, 8);
        tablemodel.setValueAt(contactupdate.ID, row, 9);

    }

    private void deleteintb(int row) {
        try {
            tablemodel.removeRow(row);
            rs.absolute(seek((Integer) jTBContact.getValueAt(row, 9)));
            rs.next();
            rs.deleteRow();
        } catch (SQLException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private ResultSet SQLexcute(String string) {
        try {
            ResultSet tmp;
            stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            tmp = stmt.executeQuery(string);
            return tmp;
        } catch (SQLException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    private void deletecontact() {
        int choose = JOptionPane.showConfirmDialog(this, "Do you want to delete all selected row(s)?", "Delete Row Warning", JOptionPane.YES_NO_OPTION);
        if (choose == JOptionPane.NO_OPTION) {
            return;
        }
        try {
            int row;
            int[] arrrow;
            pst = conn.prepareStatement("Delete Contact where ContactID=?");
            arrrow = jTBContact.getSelectedRows();
            int n = arrrow.length;
            for (int i = n - 1; i >= 0; i--) {
                pst.setInt(1, (Integer) tablemodel.getValueAt(arrrow[i], 9));
                pst.executeUpdate();
                tablemodel.removeRow(arrrow[i]);
            }
            JOptionPane.showMessageDialog(this, "Delete " + n + " Record(s) OK");
        } catch (SQLException ex) {
            Logger.getLogger(MainWindows.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
